﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using SaludOcupacional.Exceptions;
using SaludOcupacional.Model;
using System.Collections.ObjectModel;
namespace SaludOcupacional.DataAccess
{
    public class RespiratorioDataAccess : IDisposable
    {
        private DatabaseManager dbManager;

        public void Dispose()
        {
            dbManager.Dispose();
        }
        public RespiratorioDataAccess()
        {
            dbManager = new DatabaseManager();
        }

        public Respiratorio BuscarAnalisisPorCi(int ci)
        {
            if(ci == 0)
            {
                throw new ArgumentNullException("ci");
            }
            // Create a Sql sentence
            string sqlsentence = "SELECT * FROM [AnalisisRespiratorio] WHERE ci_empleado = @Ci";

            dbManager.Command.CommandText = sqlsentence;
            dbManager.Command.Parameters.AddWithValue("@Ci", ci);

            Respiratorio resp = null;

            try
            {
                DataSet dataSet = dbManager.Query();

                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    DataRow reader = dataSet.Tables[0].Rows[0];

                    resp = new Respiratorio
                    {
                        CodAnalisis = Convert.ToInt32(reader["cod_analisis"], System.Globalization.CultureInfo.InvariantCulture),
                        CiEmpleado = Convert.ToInt32(reader["ci_empleado"], System.Globalization.CultureInfo.InvariantCulture),
                        Radiografia = reader["radiografia_torax"].ToString(),
                        Obstructiva = reader["grado_obstructiva"].ToString(),
                        Restrictiva = reader["grado_restrictiva"].ToString(),
                        Nota = reader["nota_radiografia"].ToString(),
                        Accion = reader["accion_atomar"].ToString(),
                        Observaciones = reader["observaciones"].ToString(),
                        Fecha = reader["fecha"].ToString()
    
                    };
                }
            }
            catch (Exception)
            {
                throw;
            }

            return resp;
        }

        public void RegistrarAnalisis(Respiratorio respiratorio)
        {
            if (respiratorio == null)
            {
                throw new LoginException("Invalid Input Parameters");
            }

            string sqlSentence = "INSERT INTO [dbo].[AnalisisRespiratorio]([ci_empleado],[radiografia_torax],[grado_obstructiva],[grado_restrictiva],[nota_radiografia],[accion_atomar],[observaciones],[fecha])VALUES ( ";
            sqlSentence += "@Ci, @radiografia, @obstructiva, @restrictiva, @nota, @accion, @observ, @fec )";

            dbManager.Command.CommandText = sqlSentence;
            dbManager.Command.CommandType = System.Data.CommandType.Text;



            dbManager.Command.Parameters.AddWithValue("@Ci", respiratorio.CiEmpleado);
            dbManager.Command.Parameters.AddWithValue("@radiografia", respiratorio.Radiografia);
            dbManager.Command.Parameters.AddWithValue("@obstructiva", respiratorio.Obstructiva);
            dbManager.Command.Parameters.AddWithValue("@restrictiva", respiratorio.Restrictiva);
            dbManager.Command.Parameters.AddWithValue("@nota", respiratorio.Nota);
            dbManager.Command.Parameters.AddWithValue("@accion", respiratorio.Accion);
            dbManager.Command.Parameters.AddWithValue("@observ", respiratorio.Observaciones);
            dbManager.Command.Parameters.AddWithValue("@fec", respiratorio.Fecha);
            
            try
            {
                int value = dbManager.NonQuery();

                if (value < 1)
                {
                    throw new LoginException("The record wasn't registered");
                }
            }
            catch (Exception e)
            {
                throw new LoginException("Error creating a user" + e.Message, e);
            }
        }

        public void EditarAnalisis(Respiratorio respiratorio)
        {
            if (respiratorio == null)
            {
                throw new LoginException("The Analisis can´t be null");
            }

            string sqlSentence =
                "UPDATE [AnalisisRespiratorio]  SET [radiografia_torax] = @radiografia, [grado_obstructiva] = @obstructiva, [grado_restrictiva] = @restrictiva, [nota_radiografia] = @nota, [accion_atomar] = @accion, ";
            sqlSentence = sqlSentence + "[observaciones] = @observ, [fecha] = @fec ";
            sqlSentence = sqlSentence + " WHERE [ci_empleado] = @ci";

            dbManager.Command.CommandText = sqlSentence;
            dbManager.Command.CommandType = System.Data.CommandType.Text;


            dbManager.Command.Parameters.AddWithValue("@radiografia", respiratorio.Radiografia);
            dbManager.Command.Parameters.AddWithValue("@obstructiva", respiratorio.Obstructiva);
            dbManager.Command.Parameters.AddWithValue("@restrictiva", respiratorio.Restrictiva);
            dbManager.Command.Parameters.AddWithValue("@nota", respiratorio.Nota);
            dbManager.Command.Parameters.AddWithValue("@accion", respiratorio.Accion);
            dbManager.Command.Parameters.AddWithValue("@observ", respiratorio.Observaciones);
            dbManager.Command.Parameters.AddWithValue("@fec", respiratorio.Fecha);
            dbManager.Command.Parameters.AddWithValue("@ci", respiratorio.CiEmpleado);
            

            try
            {
                int value = dbManager.NonQuery();
                if (value < 1)
                {
                    throw new LoginException("The change wasn't registered");
                }

            }
            catch (Exception e)
            {
                throw new LoginException("Error edit profile", e);
            }
        }
    }
}
